package com.taojiazheng.serviceImpl.user;

import com.taojiazheng.common.ApplicationException;
import com.taojiazheng.dao.user.FindUserDao;
import com.taojiazheng.dao.user.UserDao;
import com.taojiazheng.dao.user.UserEvaluateDao;
import com.taojiazheng.entity.Finduser;
import com.taojiazheng.entity.User;
import com.taojiazheng.entity.Userinfo;
import com.taojiazheng.mapper.UserinfoMapper;
import com.taojiazheng.service.user.FindUserService;
import com.taojiazheng.so.user.FindUserSo;
import com.taojiazheng.vo.user.FindUserVo;
import com.taojiazheng.vo.user.UserEvaluateVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 赵成林
 * @date 2017/11/24 23:08
 */
@Service
public class FindUserServiceImpl implements FindUserService {
    @Autowired
    private FindUserDao findUserDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserEvaluateDao userEvaluateDao;
    @Autowired
    private UserinfoMapper userinfoMapper;
    
    public int createFindUser(FindUserSo findUserSo, String token) {
        //return 0;
        User user = userDao.getUserByToken(token);
        Finduser finduser = new Finduser();
        finduser.setInfo(findUserSo.getInfo());
        finduser.setType(findUserSo.getUserType());
        finduser.setUserId(user.getId());
        if(!isUserInfo(user.getId())) {
        	throw new ApplicationException(222,"请先实名认证，再来发布需求");
        }
        return findUserDao.createFindUser(finduser);
    }

    public List<FindUserVo> getFindUserList() {
        return findUserDao.getFinduserList();
    }

    public FindUserVo getFindUserinfo(FindUserSo findUserSo) {
        //return null;
        FindUserVo findUserVo = findUserDao.getFindUser(findUserSo);
        findUserVo.setList(this.getUserEvaluates(findUserSo));
        float avgScore = this.getAvgScore(this.getUserEvaluates(findUserSo));
        findUserVo.setAvgScore(avgScore);
        return findUserVo;
    }

    public List<UserEvaluateVo> getUserEvaluates(FindUserSo findUserSo) {
        //return null;
        return userEvaluateDao.getUserEvaluates(findUserSo.getUserId());
    }

    /**
     * 求平均分
     */
    protected float getAvgScore(List<UserEvaluateVo> userEvaluateVoList){
        float sumScore = 0.0f;
        float avgScore = 0.0f;
        if(userEvaluateVoList != null && userEvaluateVoList.size()>0){
            for (UserEvaluateVo userEvaluateVo : userEvaluateVoList){
                sumScore += userEvaluateVo.getScore();
            }
            avgScore =(float)(sumScore * 1.0 / userEvaluateVoList.size());
            avgScore = new BigDecimal(avgScore).setScale(1,BigDecimal.ROUND_HALF_UP).floatValue();
        }

        return avgScore;
    }
    
    /**
     * 判断是否已经完善个人信息
     * @param userId
     * @return
     */
    protected boolean isUserInfo(Integer userId){

        Userinfo userinfo = userinfoMapper.getUserByUserId(userId);
        if(null == userinfo){
            return false;
        }
        return true;
    }
}
